// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import com.fowo.api.entity.ListingFather;
import com.fowo.api.model.stock.out.StockOutCreateReceivableAction;
import com.fowo.api.model.stock.out.StockOutPushStockInAndStockOutAction;
import com.fowo.api.model.stock.out.StockOutPushStockOutAction;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.model.stock.out.finished.product.StockOutFinishedProductVo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import com.fowo.api.model.warehousebinrules.WarehouseBinRulesItemVo;
import com.fowo.api.model.warehousebinrules.WarehouseBinRulesSearchParamPo;
import com.fowo.api.sys.mapper.SysDictItemMapper;
import com.fowo.api.sys.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 成品出库单 服务实现类
 */
@Service
public class StockOutFinishedProductServiceImpl
  extends StockOutFinishedProductBaseServiceImpl {

    @Autowired(required = false)
    protected StockOutPushStockOutAction stockOutPushStockOutAction;

    @Autowired(required = false)
    protected StockOutPushStockInAndStockOutAction stockOutPushStockInAndStockOutAction;

    @Autowired(required = false)
    protected StockOutCreateReceivableAction stockOutCreateReceivableAction;
    @Resource
    private  WarehouseBinRulesServiceImpl warehouseBinRulesService;
    @Resource
    private SysDictMapper sysDictMapper;
    @Resource
    private SysDictItemMapper sysDictItemMapper;

    @Resource
    private ListingFatherServiceImpl listingFatherService;
    /**
     * 下推其他入库单
     */
    @Override
    public void pushStockOut(Long id) throws Exception {
        if (stockOutPushStockOutAction == null) {
            throw new Exception("此操作当前不可用");
        }
        stockOutPushStockOutAction.run(id);
    }

    /**
     * 下推生成入库单和出库单(工厂)
     */
    @Override
    public void pushStockInAndStockOut(Long id) throws Exception {
        if (stockOutPushStockInAndStockOutAction == null) {
            throw new Exception("此操作当前不可用");
        }
        stockOutPushStockInAndStockOutAction.run(id);
    }

    /**
     * 生成应收单
     */
    @Override
    public void createReceivable(Long id) throws Exception {
        if (stockOutCreateReceivableAction == null) {
            throw new Exception("此操作当前不可用");
        }
        stockOutCreateReceivableAction.run(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(StockOutFinishedProductVo model) throws Exception {
        //设置fnsku
        if(model.getStockOutProducts().size()>0){
            for(int i=0;i<model.getStockOutProducts().size();i++){
                StockOutProductItemVo x = model.getStockOutProducts().get(i);
                if( x.getFnskuId()!=null) {
                    ListingFather byId = listingFatherService.getById(x.getFnskuId());
                    x.setFnsku(byId.getFnsku());
                }
                //次品
                if(x.getProductBadNum()!=null&&x.getProductBadNum()>0&&x.getStorageBinBadId()==null){
                    WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo=new WarehouseBinRulesSearchParamPo();
                    warehouseBinRulesSearchParamPo.setWarehouseId(model.getWarehouse());
                    warehouseBinRulesSearchParamPo.setFnSku(x.getFnsku());
                    warehouseBinRulesSearchParamPo.setWarehouseId(model.getWarehouse());
                    warehouseBinRulesSearchParamPo.setProductId(x.getProductId());
                    warehouseBinRulesSearchParamPo.setShopId(x.getSid());
                    warehouseBinRulesSearchParamPo.setType(2);
                    try {
                        WarehouseBinRulesItemVo search = warehouseBinRulesService.search(warehouseBinRulesSearchParamPo);
                        x.setStorageBinBadId(search.getId());
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }
                if(x.getStorageBinGoodId()==null){
                    WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo=new WarehouseBinRulesSearchParamPo();
                    warehouseBinRulesSearchParamPo.setWarehouseId(model.getWarehouse());
                    warehouseBinRulesSearchParamPo.setFnSku(x.getFnsku());
                    warehouseBinRulesSearchParamPo.setWarehouseId(model.getWarehouse());
                    warehouseBinRulesSearchParamPo.setProductId(x.getProductId());
                    warehouseBinRulesSearchParamPo.setShopId(x.getSid());
                    warehouseBinRulesSearchParamPo.setType(1);
                    try {
                        WarehouseBinRulesItemVo search = warehouseBinRulesService.search(warehouseBinRulesSearchParamPo);
                        x.setStorageBinGoodId(search.getId());
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }
            }
        }
        Long aLong = super.create(model);
        return aLong;
    }
}
